package com.drojian.demo3.dataStorage.sharedPreferences;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.drojian.demo.R;

public class SharedPreferenceActivity extends AppCompatActivity {
    private EditText name_text;
    private EditText name_age;
    private EditText name_married;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.part3_activity_shared_preferrnce);
        Button button = findViewById(R.id.SharedPreference_btn);
        Button button2 = findViewById(R.id.SharedPreference_btn2);
        name_text = findViewById(R.id.SharedPreference_edit1);
        name_age = findViewById(R.id.SharedPreference_edit2);
        name_married = findViewById(R.id.SharedPreference_edit3);
        button.setOnClickListener(v->{
                SharedPreferences.Editor editor = getSharedPreferences("data2", MODE_PRIVATE).edit();
                editor.putString("name","xjq");
                editor.putInt("age",18);
                editor.putBoolean("married",false);
                editor.apply();
        });
        button2.setOnClickListener(v->{
            SharedPreferences get = getSharedPreferences("data2",MODE_PRIVATE);
            String name = get.getString("name", null);
            int age = get.getInt("age",0);
            boolean married = get.getBoolean("married", false);
            name_text.setText("姓名："+name);
            name_age.setText("年龄为："+age);
            name_married.setText("婚姻状况为："+married);
        });

    }
}